package tedexis.mercurio.transformers;

import org.apache.log4j.Logger;

import gurulab.maya.common.PhoneAddress;
import gurulab.merlin.core.envelope.Envelope;
import gurulab.merlin.core.router.EnvelopeTransformer;

public class MovistarTransformer implements EnvelopeTransformer {

	private Logger logger = Logger.getLogger (MovistarTransformer.class);
	private final String PEER = "movistar";
	
	public void transform(Envelope envelope) {		

		if(envelope.getDestinationRoute()!=null && envelope.getDestinationRoute().getPeer().equalsIgnoreCase(PEER)){
			//Va hacia la operadora
			//Transformacion del SMPPOperator
			PhoneAddress phoneAdd = (PhoneAddress)envelope.getDestination ();
			phoneAdd.setCrude (false);
			phoneAdd.setSchema("phone");
			//phoneAdd.setNumber ("0270414" + phoneAdd.getLocal());
			if(phoneAdd.getAreaCode().indexOf('0')!=-1){
				//Tiene el 0
				phoneAdd.setNumber (phoneAdd.getAreaCode()+phoneAdd.getLocal());
			}else{
				phoneAdd.setNumber ("0" + phoneAdd.getAreaCode()+phoneAdd.getLocal());	
			}		
			envelope.setDestination (phoneAdd);			
		}
		if(envelope.getOriginRoute()!=null && envelope.getOriginRoute().getPeer().equalsIgnoreCase(PEER)){
			//Recibe de la operadora
			//Transformacion del SmppEP
			PhoneAddress phoneAdd = (PhoneAddress)envelope.getOrigin();
			phoneAdd.setCrude (false);
			phoneAdd.setSchema("phone");
			phoneAdd.setDomain("58");
			phoneAdd.setPort("414");
			phoneAdd.setLocal(phoneAdd.getNumber().substring(4));//0414xxxxxxx
			envelope.setOrigin(phoneAdd);						
		}
		
		logger.info ("Envelope=" + envelope.toStringNoContent ());
	}	
}
